package nrd;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Verify that TheOrangeRobot shoots at enemies that collide with it.
 * 
 * @author Nathan Dorman
 * 
 */
public class TestTheOrangeRobotWhenHit extends RobotTestBed {

  boolean firesOnContact = false;

  /**
   * Designate which robots to use in this test.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.RamFire,nrd.TheOrangeRobot";
  }

  /**
   * Determine if the robots are hitting and if TheOrangeRobot has shot at the enemy.
   * 
   * @param event holds details about the end of the turn
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    IRobotSnapshot enemy = event.getTurnSnapshot().getRobots()[0];

    double heat = robot.getGunHeat();

    double xRobot = robot.getX();
    double yRobot = robot.getY();
    double xEnemy = enemy.getX();
    double yEnemy = enemy.getY();

    // checks if robots are hitting with a 5 pixel error margin
    // Robot must have fired meaning test is successful on contact
    if (Math.abs(xRobot - xEnemy) <= 5 && Math.abs(yRobot - yEnemy) <= 5 && heat > 0) {
      firesOnContact = true;

    }

  }

  /**
   * Reports the results of this battle.
   * 
   * @param event can be ignored
   */
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Robot fires on contact.", firesOnContact);
  }
}
